package com.ls.lserp.modules.job.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.dbaccess.Dao.ContractCustomEntityMapper;
import com.ls.lserp.dbaccess.Dao.ContractCustomOrderEntityMapper;
import com.ls.lserp.dbaccess.Dao.OrderBrandEntityMapper;
import com.ls.lserp.dbaccess.Dao.OrderEntityMapper;
import com.ls.lserp.dbaccess.Dao.StyleEntityMapper;
import com.ls.lserp.dbaccess.Entity.ContractCustomEntity;
import com.ls.lserp.dbaccess.Entity.ContractCustomOrderEntity;
import com.ls.lserp.dbaccess.Entity.OrderBrandEntity;
import com.ls.lserp.dbaccess.Entity.OrderEntity;
import com.ls.lserp.dbaccess.Entity.StyleEntity;

/**
 * @ClassName : DataBaseSyncService
 * @Description : ERP与平台数据同步 a_designoption_p
 * @author : zhigang.gao
 * @date : 2017年9月21日
 */
@Service
@Transactional(readOnly = false)
public class MUOrderSyncService {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired // 1.c_contract_custom_p 定制合同表
	private ContractCustomEntityMapper contractCustomEntityMapper;

	@Autowired // 2.c_contract_custom_order_r 定制合同订单关联表
	private ContractCustomOrderEntityMapper contractCustomOrderEntityMapper;

	@Autowired // 3.o_order_p 定制订单表
	private OrderEntityMapper orderEntityMapper;

	@Autowired // 4.s_style_p 定制款式表
	private StyleEntityMapper styleEntityMapper;
	
	@Autowired // o_order_brand_r(平台表)
	private OrderBrandEntityMapper orderBrandEntityMapper;

	/**
	 * 获取合同数据并更新同步状态
	 * 
	 * @Title : findContractData
	 * @Description : TODO
	 * @return
	 * @throws Exception
	 * @return : List<ContractCustomEntity>
	 *
	 * @author : GavinGao
	 * @date : 2018年8月1日
	 */
	public List<ContractCustomEntity> findContractData() throws Exception {
		List<ContractCustomEntity> contractCustomList = contractCustomEntityMapper.findContractMTMList(new ContractCustomEntity());
		updateSyncStatus(contractCustomList);
		return contractCustomList;
	}

	/**
	 * 获取同步数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getSyncData(List<ContractCustomEntity> contractCustomList) throws Exception {
		logger.info("/-------------------------数据读取-------------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")
				+ "--------------开始--------------------------------------------------/");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// o_order_p
		List<OrderEntity> orderList = new ArrayList<OrderEntity>();
		// s_style_p
		List<StyleEntity> styleList = new ArrayList<StyleEntity>();
		// 存在数据则继续执行
		if (contractCustomList != null && contractCustomList.size() > 0) {
			// 遍历获取其他合同相关数据
			for (ContractCustomEntity contractEntity : contractCustomList) {
				// 获取合同订单关联数据
				List<ContractCustomOrderEntity> contractCustomOrderTempList = contractCustomOrderEntityMapper
						.findListByContractId(contractEntity.getContractId());
				// 存在数据则继续执行
				if (contractCustomOrderTempList != null && contractCustomOrderTempList.size() > 0) {
					// 遍历获取订单及其他相关数据
					for (ContractCustomOrderEntity customOrderEntity : contractCustomOrderTempList) {
						// 获取订单信息数据
						OrderEntity orderEntity = orderEntityMapper.get(customOrderEntity.getOrderId());
						if (orderEntity != null) {
							// 荷兰订单
							if (contractEntity.getCustomerId() == 78) {
								// MUNRO B.V 店铺id
								orderEntity.setShopId(41);
								// MUNRO B.V 店铺管理员id
								orderEntity.setCreateUser(71);
							// 日本大货
							}else{
								orderEntity.setShopId(Integer.parseInt(orderEntity.getShopName()));
							}
							// 设置数据
							orderEntity.setOgnizationId(contractEntity.getCustomerId().intValue());
							orderEntity.setCustomerId(contractEntity.getCustomerId().intValue());
							orderEntity.setOrderNumber(new BigDecimal(1));// 订单数量
							orderEntity.setOrderStatusCd("ORDER_STATUS-OS_40");// 订单状态：生产完成
							orderEntity.setCreateDate(contractEntity.getCreateDateTime());
							// 将订单数据放入对应总集合对象中
							orderList.add(orderEntity);
							// 获取款式信息数据
							StyleEntity styleEntity = styleEntityMapper.getStyleInfo(orderEntity.getStyleId().longValue());
							if (styleEntity != null) {
								styleEntity.setShopId(orderEntity.getShopId());
								// 将款式数据放入对应总集合对象中
								styleList.add(styleEntity);
								logger.info("/-------数据读取-------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "------------"
										+ orderEntity.getOrderNo() + "------------/");
							}
						}
					}
				}
			}
		}
		resultMap.put("orderList", orderList);
		resultMap.put("styleList", styleList);
		logger.info("/-------------------------数据读取-------------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")
				+ "--------------结束--------------------------------------------------/");
		return resultMap;
	}
	
	/**
	 * 更新同步状态
	 * @param contractCustomList
	 * @throws Exception
	 */
	public void updateSyncStatus(List<ContractCustomEntity> contractCustomList) throws Exception {
		if (contractCustomList != null) {
			for (ContractCustomEntity entity : contractCustomList) {
				contractCustomEntityMapper.updateSyncStatusMTM(entity);
			}
		}
		logger.info("/-------更新同步状态-------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "------------/");
	}

	/**
	 * 插入同步数据
	 * 
	 * @param dataMap
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void insertData(Map<String, Object> dataMap) throws Exception {
		logger.info("/-------------------------数据写入-------------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")
		+ "--------------开始--------------------------------------------------/");
		if (dataMap != null && dataMap.size() > 0) {
			// o_order_p
			List<OrderEntity> orderList = (List<OrderEntity>) dataMap.get("orderList");
			// s_style_p
			List<StyleEntity> styleList = (List<StyleEntity>) dataMap.get("styleList");
			if (styleList != null && styleList.size() > 0) {
				for(int i = 0; i < styleList.size(); i++){
					// 款式插入
					styleEntityMapper.insertMTM(styleList.get(i));
					// 设置新款式ID
					orderList.get(i).setStyleId(styleList.get(i).getSysStyleId());
					// 订单插入
					orderEntityMapper.insertMTM(orderList.get(i));
					// 客户店铺订单关联表
					OrderBrandEntity orderBrandEntity = new OrderBrandEntity();
					orderBrandEntity.setOgnizationId(orderList.get(i).getOgnizationId().longValue());
					orderBrandEntity.setShopId(orderList.get(i).getShopId().longValue());
					orderBrandEntity.setOrderId(orderList.get(i).getOrderId().longValue());
					// 关联表插入
					orderBrandEntityMapper.insert(orderBrandEntity);
					
					logger.info("/------数据量:" + styleList.size() + "-----------第"+(i+1)+"个-----数据写入---结束----"
							+ DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "------------/");
				}
				
			}
		}
		logger.info("/-------------------------数据写入-------------" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")
		+ "--------------结束--------------------------------------------------/");
	}
}
